package com.zyd.controller.safe;

import com.zyd.constants.ErrorDef;
import com.zyd.exception.BusinessException;
import com.zyd.io.model.MealEntity;
import com.zyd.io.page.PageBean;
import com.zyd.io.page.PageRequestBean;
import com.zyd.io.vo.page.MultiQueryPageVO;
import com.zyd.io.vo.res.ResBody;
import com.zyd.service.meal.MealService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/safes")
public class SafeController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SafeController.class);

    @Autowired
    private MealService service;


    @PostMapping(value = "/findSafe", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findSafe(@RequestBody MultiQueryPageVO request) {
        try {
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询保险成功");
            PageBean<Map<String, Object>> ret = service.findMealPage(request);
            resBody.setData(ret);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询保险失败");
        }
    }


    @PostMapping(value = "/renewal", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody renewal(@RequestBody MealEntity request) {
        try {
            service.addMeal(request);
            return new ResBody(ErrorDef.SUCCESS, "车辆续保成功");
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "车辆续保失败");
        }
    }
}
